/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package kr.co.casually;

import java.util.Date;

import kr.co.casually.preference.ConcertPreferences;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

public class NotificationTimeSetActivity extends Activity {

	Button btnOK;
	Button btnCancel;
	private SharedPreferences mPrefs;
	private Context mContext;
	private int mHour;
	private int mMinute;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;

		setContentView(R.layout.notification_time_set_act);
		btnCancel = (Button) findViewById(R.id.noti_time_set_cancel_btn);
		btnOK = (Button) findViewById(R.id.noti_time_set_ok_btn);

		Date date = new Date();

		TimePicker timePicker = (TimePicker) findViewById(R.id.timePicker);
		timePicker.setCurrentHour(date.getHours());
		timePicker.setCurrentMinute(date.getMinutes());

		timePicker
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {
						mHour = hourOfDay;
						mMinute = minute;
					}
				});

		btnOK.setOnClickListener(mClickListener);
		btnCancel.setOnClickListener(mClickListener);
	}

	OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.noti_time_set_ok_btn:
				setNoitTime(mHour, mMinute);
				break;
			case R.id.noti_time_set_cancel_btn:
				finish();
				break;

			default:
				break;
			}
		}
	};

	private void setNoitTime(int hour, int minute) {
		mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
		ConcertPreferences.storeNotiTime(mContext, hour, minute);

		Toast.makeText(
				mContext,
				mPrefs.getInt(ConcertPreferences.NOTI_HOUR, 0) + " - "
						+ mPrefs.getInt(ConcertPreferences.NOTI_MINUTE, 0),
				1000).show();

	}
}
